var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
const session=require("express-session");
const filestore=require("session-file-store")(session);
const sessionMiddleWare=session({
  // store:new filestore(),
  secret:"可以是任何密码",
  cookie:{maxAge:600000}
});
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

// var fs=require("fs");
// var path=require("path");
// //读取db文件,实际上等于引入一个db的json对象
// const db=require("./db.json");
// const wPath=path.join(__dirname,"db.json");
// //创建解析中间件
// var multer=require("multer");
// var upload=multer({dest:"uploads/"});

//写入db文件，db本身是个对象，转换为字符串才可以写入
// fs.writefile(Wpath,JSON.stringify(db));

var index = require('./routes/index');
var users = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(sessionMiddleWare);


app.use('/', index);
app.use('/users', users);
app.use('/work',require("./routes/work"));
app.use("/test",require("./routes/test"));
//上传图片
// app.post("/up",upload.single("upImg"),function (req,res){
//   const file=req.file;
//   file.filename
// });

// //创建相册
// app.post("/album/create",function (req,res){
//
// });

// //查找图片
// app.get("/init",function (req,res){
//   const types=db.types;
//   res.send({types,phtots:db.photos.default});
// });

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});


module.exports = app;
